import { instance, isPlainObject } from "../helpers";

export default function ({ dispatch }) {
    return function (next) {
        return function (action) {
            // 如果action是Promise实例或者action是对象并且payload是promise实例
            let isActionPromise = instance(action, Promise);
            if (isActionPromise || (isPlainObject(action) && instance(action.payload, Promise))) {
                // 处理派发时promise的
                console.log(dispatch, action)
                if (isActionPromise) {
                    action.then(res => {
                        dispatch(res);
                    })
                } else {
                    // 处理是payload是promise的情况
                    action.payload.then(payload => dispatch({ ...action, payload }))
                }
            } else {
                next(action);
            }
        }
    }
} 